
#include <vector>
#include "DebugComponent.h"
#include "RenderableEntity.h"
#include "InputManager.h"

DebugComponent::DebugComponent()
{
    m_buttons.push_back(new RenderableEntity("icon_babycoin", Vector2(100, 100)));
    m_buttons.push_back(new RenderableEntity("icon_babycoin", Vector2(100, 200)));
    m_buttons.push_back(new RenderableEntity("icon_babycoin", Vector2(200, 100)));
    m_buttons.push_back(new RenderableEntity("icon_babycoin", Vector2(300, 100)));
    m_buttons.push_back(new RenderableEntity("icon_babycoin", Vector2(400, 100)));
    m_buttons.push_back(new RenderableEntity("icon_babycoin", Vector2(300, 200)));
    m_buttons.push_back(new RenderableEntity("icon_babycoin", Vector2(500, 100)));
    m_buttons.push_back(new RenderableEntity("icon_babycoin", Vector2(500, 200)));
    
    m_iStep = 1;
}

DebugComponent::~DebugComponent()
{
}

void DebugComponent::addEntity(RenderableEntity* _entity)
{
	m_entities.push_back(_entity);
    
    selectEntity(m_iCurrentEntity);
}

void DebugComponent::clear()
{
	m_entities.clear();
    m_iCurrentEntity = 0;
}

void DebugComponent::selectEntity(int _iIndex)
{
	m_iCurrentEntity = _iIndex;
    
	for(int i=0; i<m_entities.size(); i++)
	{
		if(i==m_iCurrentEntity)
			m_entities.at(i)->setAlpha(1.0f);
		else
			m_entities.at(i)->setAlpha(0.5f);
	}
}

bool posInRect(Vector2 pos, int x, int y, int width, int height)
{
    return pos.x > x && pos.x < x + width && pos.y > y && pos.y < y + height;
}

extern "C" void debugLog(const char* text);

void DebugComponent::update(float dt)
{
	if(m_entities.size() <= 0)
		return;

    bool bJustPressed = InputManager::instance().hasJustReleased();
    bool bPressed = InputManager::instance().isPressed();
    
    Vector2 vPos = InputManager::instance().getPosition();

	if(bJustPressed && posInRect(vPos, 50, 50, 40, 40))
	{
		selectEntity((m_iCurrentEntity + 1) % m_entities.size());
	}
	
	if(bJustPressed && posInRect(vPos, 50, 100, 40, 40))
	{
		selectEntity((m_iCurrentEntity - 1 + m_entities.size()) % m_entities.size());
	}
	
    if(bPressed && posInRect(vPos, 100, 50, 40, 40))
        m_entities[m_iCurrentEntity]->translate(Vector2(-m_iStep, 0));
    if(bPressed && posInRect(vPos, 200, 50, 40, 40))
        m_entities[m_iCurrentEntity]->translate(Vector2(m_iStep, 0));
    if(bPressed && posInRect(vPos, 150, 50, 40, 40))
        m_entities[m_iCurrentEntity]->translate(Vector2(0, -m_iStep));
    if(bPressed && posInRect(vPos, 150, 100, 40, 40))
        m_entities[m_iCurrentEntity]->translate(Vector2(0, m_iStep));

    if(bJustPressed && posInRect(vPos, 250, 100, 40, 40))
        m_iStep++;
    if(bJustPressed && posInRect(vPos, 250, 50, 40, 40))
        m_iStep--;
	
	if(bJustPressed && posInRect(vPos, 0, 240, 320, 240))
	{
        char buff[100];
        sprintf(buff, "Pos: %f, %f\n", m_entities[m_iCurrentEntity]->getPosition().x, m_entities[m_iCurrentEntity]->getPosition().y);
        std::string text = "";
        text.append(buff);
		debugLog(text.c_str());
	}
}

void DebugComponent::render()
{
    for(int i=0; i<m_buttons.size(); i++)
    {
        m_buttons.at(i)->render();
    }
}
